//
//  YMCameraPreviewView.m
//  YMMedical
//
//  Created by y s on 2023/7/24.
//

#import "YMCameraPreviewView.h"


@implementation YMCameraPreviewBox

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    
    CGSize viewSize = self.bounds.size;
    
    CGFloat lineHeight = [SFitUtil fit:6];
    CGFloat lineWidth = [SFitUtil fit:20];
    CGFloat lengths[] = {5,5};
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextAddLineToPoint(ctx, lineWidth, 0);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(ctx, lineHeight);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, viewSize.width-lineWidth, 0);
    CGContextAddLineToPoint(ctx, viewSize.width, 0);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, viewSize.width, 0);
    CGContextAddLineToPoint(ctx, viewSize.width, lineWidth);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, viewSize.width, viewSize.height-lineWidth);
    CGContextAddLineToPoint(ctx, viewSize.width, viewSize.height);
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, viewSize.width, viewSize.height);
    CGContextAddLineToPoint(ctx, viewSize.width-lineWidth, viewSize.height);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, lineWidth, viewSize.height);
    CGContextAddLineToPoint(ctx, 0, viewSize.height);
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, 0, viewSize.height);
    CGContextAddLineToPoint(ctx, 0, viewSize.height-lineWidth);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, 0, lineWidth);
    CGContextAddLineToPoint(ctx, 0, 0);
    CGContextStrokePath(ctx);
    
    
    
    
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, lineWidth+2, 0);
    CGContextAddLineToPoint(ctx, viewSize.width-lineWidth-2, 0);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    CGContextSetLineDash(ctx, 0, lengths, 1);
    CGContextSetLineWidth(ctx, [SFitUtil fit:1]);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, viewSize.width, lineWidth+2);
    CGContextAddLineToPoint(ctx, viewSize.width, viewSize.height-2-lineWidth);
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, viewSize.width-lineWidth-2, viewSize.height);
    CGContextAddLineToPoint(ctx, lineWidth+2, viewSize.height);
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, 0, viewSize.height-lineWidth-2);
    CGContextAddLineToPoint(ctx, 0, lineWidth+2);
    CGContextStrokePath(ctx);


}


@end


@interface YMCameraPreviewView ()

@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) UILabel *lblSub;
@property (nonatomic, strong) YMCameraPreviewBox *vPreview;

@property (nonatomic, strong) UIButton *btnTap;


@end

@implementation YMCameraPreviewView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    [self addSubview:self.vPreview];
    [self.vPreview addSubview:self.lblTitle];
    [self.vPreview addSubview:self.lblSub];
    [self addSubview:self.btnTap];
    
    [self.vPreview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:450]);
        make.top.mas_equalTo([SFitUtil fit:39]+TOP_BAR_HEIGHT);
    }];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo([SFitUtil fit:192]);
    }];
    [self.lblSub mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.equalTo(self.lblTitle.mas_bottom).offset([SFitUtil fit:6]);
    }];
    [self.btnTap mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.greaterThanOrEqualTo(self.vPreview.mas_bottom).offset([SFitUtil fit:5]);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:54]);
    }];
}

- (void)tapClickAction{
    if (self.clickBlock){
        self.clickBlock();
    }
    [self removeFromSuperview];
}
#pragma mark -- setter and getter
- (UILabel *)lblTitle{
    if (_lblTitle == nil){
        _lblTitle = [SThemeUtil boldLabel:KLanguage(@"screen_red_line") color:STheme.H_D9 fontSize:25];
        _lblTitle.numberOfLines = 0;
        _lblTitle.textAlignment = NSTextAlignmentCenter;
    }
    return _lblTitle;
}
- (UILabel *)lblSub{
    if (_lblSub == nil){
        _lblSub = [SThemeUtil boldLabel:KLanguage(@"red_line_area") color:STheme.H_D9 fontSize:25];
        _lblSub.numberOfLines = 0;
        _lblSub.textAlignment = NSTextAlignmentCenter;
    }
    return _lblSub;
}
- (UIButton *)btnTap{
    if (_btnTap == nil){
        _btnTap = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnTap setTitle:KLanguage(@"find_line_click") forState:UIControlStateNormal];
        [_btnTap setTitleColor:STheme.D12 forState:UIControlStateNormal];
        _btnTap.titleLabel.font = [SFitUtil fitFont:16];
        [_btnTap addTarget:self action:@selector(tapClickAction) forControlEvents:UIControlEventTouchUpInside];
        _btnTap.backgroundColor = STheme.D9;
        _btnTap.layer.cornerRadius = [SFitUtil fit:10];
        _btnTap.clipsToBounds = YES;
    }
    return _btnTap;
}
- (YMCameraPreviewBox *)vPreview{
    if (_vPreview == nil){
        _vPreview = [[YMCameraPreviewBox alloc] initWithFrame:CGRectMake(0, 0, [SFitUtil fit:345], [SFitUtil fit:450])];
        _vPreview.backgroundColor = [SUIUtil colorOf:0xffffff alpha:0.1];
    }
    return _vPreview;
}

@end
